<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BWProcessData
 *
 * @author i062261
 */
class BPostManager {
    //put your code here

    /**
     * format of post data:
     * type_class_name[index] or
     * type_class_name or
     * class_name
     * @param type $modelName
     * @return array, return a list for all post data
     */
    public function parsedModelData($modelName = NULL) {
        $parsedData = array();
        $postData = array();
        if ($modelName == NULL)
            $modelName = 'BDynamicFormModel';
        if (is_string($modelName)) {
            if (isset($_POST[$modelName]))
                $postData = $_POST[$modelName];

            if (is_array($postData) && $postData != NULL) {
                //$name[$index] = $postData[$name][$index];
                foreach ($postData as $key => $value) {
                    $oName = explode("_", $key);
                    if (count($oName) < 2)
                        return;
                    $e = array();
                    if (in_array(strtolower($oName[0]), array('image', 'string', 'array'))) {
                        $type = $oName[0];
                        $class = $oName[1];
                        $name = $oName[2];
                        $e['type'] = $type;
                        $e['class'] = $class;
                        $e['name'] = $name;
                        $e['value'] = $value;
                        $index = $key;
                        $index = str_replace($oName[0] . '_', '', $index);
                        $index = str_replace($oName[1] . '_', '', $index);
                        $index = str_replace($oName[2] . '_', '', $index);

                        $e['index'] = $index;
                        //array_push($parsedData, $e);
                    } else {
                        $type = 'string';
                        $class = $oName[0];
                        $name = $oName[1];

                        //$e = array();
                        $e['type'] = $type;
                        $e['class'] = $class;
                        $e['name'] = $name;
                        $index = $key;
                        $index = str_replace($oName[0] . '_', '', $index);
                        $index = str_replace($oName[1] . '_', '', $index);
                        $e['index'] = $index;

                        $e['value'] = $value;
                    }

                    /*
                      //add inputable owner for media or message
                      if (isset($oName[2]) && strtolower($oName[2]) == 'inputable') {
                      $e['owner_inputMediaName'] = $oName[3];
                      $e['owner_inputableName'] = $oName[4];
                      }

                      //add inputable owner for media or message
                      if (isset($oName[3]) && strtolower($oName[3]) == 'inputable') {
                      $e['owner_inputMedia'] = $oName[4];
                      $e['owner_inputable'] = $oName[5];
                      }
                     * 
                     */

                    array_push($parsedData, $e);
                }
            }
        }
        return $parsedData;
    }

    public function saveInputValueFromPostData($modelName) {
        if ($modelName == NULL)
            $modelName = 'BDynamicFormModel';
        $parsedData = array();
        $postData = array();
        if (is_string($modelName)) {
            if (isset($_POST[$modelName]))
                $postData = $_POST[$modelName];

            if (is_array($postData) && $postData != NULL) {
                $inputMedia = NULL;
                foreach ($postData as $key => $value) {
                    $oName = split("_", $key);
                    $count = count($oName);
                    if ($count == 2) {
                        if (isset($oName[0]) && $oName[0] == 'iMedia') {
                            $inputMediaId = $oName[1];
                            $inputMedia = new BWInputMedia($inputMediaId);
                            $inputMedia->addInputValue($value);
                        }
                    }
                    if ($count == 3) {
                        if (isset($oName[1]) && $oName[1] == 'iMedia') {
                            $type = $oName[0];
                            $inputMediaId = $oName[2];
                            $inputMedia = new BWInputMedia($inputMediaId);
                            $inputMedia->addInputValue($value);
                        }
                    }
                }
                return $inputMedia;
            }
        }
        return false;
    }

    public function populateEventFromPostData($postData, $eventIndex = NULL, $event = null) {

        if (is_numeric($eventIndex) && $eventIndex > 0 && $event == NULL) {
            $event = new BWEvent($eventIndex);
        } else {
            if (!$event instanceof BWEvent)
                $event = new BWEvent();
        }

        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if ($class == 'event') {
                if ($eventIndex != NULL) {
                    if (property_exists('BWEvent', $name) && $type == 'string' && $index == $eventIndex) {
                        $event->$name = $value;
                    }
                } else {
                    //if not set eventId
                    if (property_exists('BWEvent', $name) && $type == 'string') {
                        $event->$name = $value;
                    }
                }
            }
        }
        return $event;
    }

    public function populateMessageListFromPostData($postData, $messageIndex = NULL, $owner = NULL) {

        $messageList = array();

        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            $message = NULL;
            if ($class == 'message') {
                if ($messageIndex != NULL && $index != NULL) {
                    if (property_exists('BWMessage', 'name') && $type == 'string' && $index == $messageIndex) {
                        $message = new BWMessage();
                        $message->name = $name;
                        $message->description = $value;
                        if ($owner instanceof BEntity && $owner->getId() != NULL) {
                            $message->ownerId = $owner->getId();
                            $message->ownerType = get_class($owner);
                        }
                    }
                } else {
                    //if not set eventId
                    if (property_exists('BWMessage', 'name') && $type == 'string') {
                        $message = new BWMessage();
                        $message->name = $name;
                        $message->description = $value;
                        if ($owner instanceof BEntity && $owner->getId() != NULL) {
                            $message->ownerId = $owner->getId();
                            $message->ownerType = get_class($owner);
                        }
                    }
                }
                if ($message instanceof BWMessage) {
                    $messageList[$name] = $message;
                }
            }
        }

        return $messageList;
    }

    public function populateMediaListFromPostData($postData, $mediaIndex = NULL, $media = NULL) {

        $list = array();

        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if ($data['name'] == "image") {
                if ($index == 'buyer_info' || $index == 'vender_info' || $index == 'product_info')
                    $type = $data['name'];
            }
            if ($class == 'media') {
                //save string of media
                if ($mediaIndex != NULL && $index != NULL) {
                    if (property_exists('BMedia', 'name') && $type == 'string' && $index == $mediaIndex) {
                        $media = new BMedia ();
                        $media->name = $name;
                        $media->content = $value;
                        $list[$name] = $media;
                    }

                    //add array of data
                    if (property_exists('BMedia', 'name') && $type == 'array' && $index == $mediaIndex) {
                        $media = new BMedia ();
                        $media->name = $name;
                        $media->content = $value;
                        $list[$name] = $media;
                    }
                    //save image of media
                    if (property_exists('BMedia', 'name') && $type == 'image' && $index == $mediaIndex) {
                        $media = new BMedia ();
                        $media->name = $name;
                        $media->type = $type;
                        $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . '_' . $index . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
                        if ($name == 'image' && $type = 'image')
                            if ($index == 'buyer_info' || $index == 'vender_info' || $index == 'product_info')
                                $media->content = 'BDynamicFormModel[' . $class . '_' . $name . '_' . $index . ']';
                        //array_push($list, $media);
                        $list[$name] = $media;
                    }
                } else {
                    //if not set mediaIndex, save string of media
                    if (property_exists('BMedia', 'name') && $type == 'string') {
                        $media = new BMedia ();
                        $media->name = $name;
                        $media->content = $value;
                        //array_push($list, $media);
                        $list[$name] = $media;
                    }

                    if (property_exists('BMedia', 'name') && $type == 'array') {
                        $media = new BMedia ();
                        $media->name = $name;
                        $media->content = $value;
                        //array_push($list, $media);
                        $list[$name] = $media;
                    }
                    //save image of media
                    if (property_exists('BMedia', 'name') && $type == 'image') {
                        $media = new BMedia ();
                        $media->type = $type;
                        $media->name = $name;
                        $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . '_' . $index . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
                        //array_push($list, $media);
                        $list[$name] = $media;
                    }
                }
            }
        }
        foreach ($list as $key => $media) {
            if ($media->type == 'string') {
                //$media->save();
            }
            if ($media->type == 'image') {
                $tempImage = CUploadedFile::getInstanceByName($media->content);
                if ($tempImage) {
                    //上传文件
                    $newImageName = time() . '_' . rand(1, 9999) . '.' . $tempImage->extensionName;
                    $path = Yii::getPathOfAlias('webroot') . '/upload/';
                    //上传目录
                    if (!is_dir($path)) {
                        mkdir($path);
                        //目录不存在则创建
                    }
                    //The image is valid, you can save it
                    $path = $path . $newImageName;
                    $tempImage->saveAs($path);
                    //$media->name = 'image';
                    $media->content = '/upload/' . $newImageName;
                    //$media->save();
                } else {
                    unset($list[$key]);
                }
            }
        }
        return $list;
    }

    public function populatePostDataByIndex($postData, $mediaIndex, $media = NULL) {
        $mediaList = $this->populateMediaListFromPostData($postData, $mediaIndex, $media);
        $rPostData = array();
        foreach ($mediaList as $media) {
            if ($media->content != NULL)
                $rPostData[$media->name] = $media->content;
        }
        return $rPostData;
    }

    public function populateEventTypeFromPostData($postData, $eventTypeIndex = 0, $type = NULL) {
        if ($type == NULL)
            $type = new BWEventGroup ();

        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if (strtolower($class) == 'eventtype') {
                $eventTypeId = NULL;
                if ($eventTypeIndex != NULL && $index != NULL) {
                    if ($eventTypeIndex == $index) {
                        if (strtolower($name) == 'eventtypeid')
                            $eventTypeId = $value;
                    }
                }else {
                    if (strtolower($name) == 'eventtypeid')
                        $eventTypeId = $value;
                }
                if ($eventTypeId != NULL && $eventTypeId > 0) {
                    $eventType = new BWEventGroup($eventTypeId);
                    return $eventType;
                }
            }
        }

        return FALSE;
    }

    public function populateInputMediaListFromPostData($postData, $inputIndex = NULL, $owner = NULL) {
        $inputMediaList = array();
        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if (strtolower($class) == 'inputmedia') {
                $inputMedia = NULL;
                //save string of media
                if ($inputIndex != NULL && $index != NULL) {
                    if (property_exists('BWInputMedia', 'name') && $type == 'string') {
                        if ($index == $inputIndex) {
                            $inputMedia = new BWInputMedia();
                            $inputMedia->name = $name;
                            $inputMedia->content = $value;
                            if ($owner instanceof BEntity && $owner->getId() != NULL) {
                                $inputMedia->ownerId = $owner->getId();
                                $inputMedia->ownerType = get_class($owner);
                            }
                            $inputMedia->save();
                            $inputMediaList[$name] = $inputMedia;
                        }
                    }
                } else {
                    //if not set mediaIndex, save string of media
                    if ($type == 'string') {
                        $inputMedia = new BWInputMedia();
                        $inputMedia->name = $name;
                        $inputMedia->content = $value;
                        if ($owner instanceof BEntity && $owner->getId() != NULL) {
                            $inputMedia->ownerId = $owner->getId();
                            $inputMedia->ownerType = get_class($owner);
                        }
                        $inputMedia->save();
                        $inputMediaList[$name] = $inputMedia;
                    }
                }


                if ($inputMedia instanceof BWInputMedia && $inputMedia->getId() != NULL) {
                    //add message for inputmedia
                    $messageList = $this->populateMessageListFromPostData($postData, 'inputMedia_' . $name);
                    foreach ($messageList as $message) {
                        $inputMedia->getMessageManager()->addMessage($message);
                        $messageName = $message->getName();
                        $mediaList = $this->populateMediaListFromPostData($postData, 'message_' . $messageName);
                        foreach ($mediaList as $media) {
                            $message->getMediaManager()->addMedia($media);
                        }
                    }

                    //add media for inputMedia
                    $mediaList = $this->populateMediaListFromPostData($postData, 'inputMedia_' . $name);
                    foreach ($mediaList as $media) {
                        $inputMedia->getMediaManager()->addMedia($media);
                    }
                }
            }
        }

        //parse inputType and save data into database
        foreach ($postData as $data) {
            $type = $data['type'];
            $class = $data['class'];
            $name = $data['name'];
            $value = $data['value'];
            $index = isset($data['index']) ? $data['index'] : NULL;
            if (strtolower($class) == 'inputtype') {
                $inputMediaName = $data['name'];
                $inputMedia = $inputMediaList[$inputMediaName];
                if ($inputMedia instanceof BWInputMedia) {
                    $inputIndex = $inputMediaName;
                }
                if ($inputIndex != NULL && $index != NULL) {
                    if ($index == $inputIndex) {
                        $typeClass = $data['value'];
                        $inputType = new $typeClass($inputMedia);
                        $inputType = $inputType->populateInputTypeByPostData($postData);
                        $inputMedia->setInputType($inputType);
                        //save data for inputtype
                        $inputMedia->save();
                    }
                } else if ($inputIndex == NULL) {
                    $typeClass = $data['value'];
                    $inputType = new $typeClass($inputMedia);
                    $inputType = $inputType->populateInputTypeByPostData($postData);
                    $inputMedia->setInputType($inputType);
                    $inputMedia->save();
                }
            }
        }
        //parsed inputable data
        /*
          foreach ($postData as $data) {
          $type = $data['type'];
          $class = $data['class'];
          $name = $data['name'];
          $subName = isset($data['subName']) ? $data['subName'] : NULL;
          $value = $data['value'];
          $index = isset($data['index']) ? $data['index'] : NULL;
          if (strtolower($class) == 'inputable') {
          if ($inputIndex != NULL && $index != NULL) {
          if ($index == $inputIndex) {
          $inputMediaName = $data['name'];
          $inputMedia = $inputMediaList[$inputMediaName];
          $inputType = $inputMedia->getInputType();
          if ($inputType instanceof BWKeyValueType) {
          if ($subName != NULL) {
          $inputType->addInputable($subName, $value);
          }
          }
          }
          } else {
          $inputMediaName = $data['inputMedia'];
          $inputMedia = $inputMediaList[$inputMediaName];
          $inputType = $inputMedia->getInputType();
          if ($inputType instanceof BWKeyValueType) {
          $inputType->addInputable($name, $value);
          }
          }
          }
          }
         * 
         */

        //parse media belong to inputable value
        /*
          foreach ($postData as $data) {
          $type = $data['type'];
          $class = $data['class'];
          $name = $data['name'];
          $value = $data['value'];
          $index = isset($data['index']) ? $data['index'] : NULL;
          if ($class == 'media') {
          //save string of media
          if ($inputIndex != NULL && $index != NULL) {
          if (property_exists('BMedia', 'name') && $type == 'string' && $index == $inputIndex) {
          $media = new BMedia ();
          $media->name = $name;
          $media->content = $value;
          //array_push($list, $media);
          }
          //save image of media
          if (property_exists('BMedia', 'name') && $type == 'image' && $index == $inputIndex) {
          $media = new BMedia ();
          $media->name = $name;
          $media->type = $type;
          $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . '_' . $index . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
          //array_push($list, $media);
          }
          } else {
          //if not set mediaIndex, save string of media
          if (property_exists('BMedia', 'name') && $type == 'string') {
          $media = new BMedia ();
          $media->name = $name;
          $media->content = $value;
          //array_push($list, $media);
          }
          //save image of media
          if (property_exists('BMedia', 'name') && $type == 'image') {
          $media = new BMedia ();
          $media->type = $type;
          $media->name = $name;
          $media->content = 'BDynamicFormModel[' . $type . '_' . $class . '_' . $name . ']'; //构造image的input的name：BDynamicFormModel[image_media_image_0]
          //array_push($list, $media);
          }
          }

          //add media for inputable value
          if (isset($data['owner_inputableName'])) {
          $ownerInputableName = $data['owner_inputableName'];
          $ownerInputMediaName = $data['owner_inputMediaName'];
          $inputMedia = $inputMediaList[$ownerInputMediaName];
          $inputType = $inputMedia->getInputType();
          if ($inputType instanceof BWKeyValueType) {
          $inputType->addMediaByInputableValue($media, $ownerInputMediaName);
          }
          }
          }
          }
         * 
         */

        return $inputMediaList;
    }

}
